package com.hu.ssm.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hu.ssm.pojo.Books;
import com.hu.ssm.service.impl.BookServiceImpl;

@Controller
@RequestMapping("/book")
public class BookController {

	@Autowired
	@Qualifier("bookservice")
	private BookServiceImpl mapper;

	// 查询全部书籍并返回一个前端页面
	@RequestMapping("/allbook")
	public String save(Model model) {
		List<Books> list = mapper.querySelect();
		model.addAttribute("list", list);
		return "list";
	}

	// 跳转到添加书籍页面
	@RequestMapping("/toaddbook")
	public String addpage() {
		return "addbook";
	}

	// 添加书籍
	@RequestMapping("/addbook")
	public String add(Model model, Books books) {
		mapper.add(books);
		return "redirect:/book/allbook";// 重定向
	}

	// 跳转到修改书籍页面
	@RequestMapping("/toupdatebook")
	public String updatepage(int id, Model model) {
		Books books = mapper.queryById(id);
		model.addAttribute("book", books);
		return "updatebook";
	}

	// 修改的书籍
	@RequestMapping("/updatebook")
	public String update(Books books) {
		mapper.update(books);
		return "redirect:/book/allbook";
	}

	// 删除书籍
	@RequestMapping("/delbook")
	public String del(int bookid) {
		mapper.deleteBookById(bookid);
		return "redirect:/book/allbook";// 重定向
	}

	// 查询书籍的名称
	@RequestMapping("/querybookname")
	public String querybookname(String bookname, Model model) {
		Books querybookname = mapper.querybookname(bookname);
		List<Books> list = new ArrayList<>();
		if (querybookname == null) {
			model.addAttribute("err", "未查到");
			return "list";
		}
		list.add(querybookname);
		model.addAttribute("list", list);
		return "list";
	}

	@RequestMapping("/imgfile")
	public String filepage() {
		return "imgfile";
	}
}
